package core;

import gitobject.Commit;
import gitobject.JitIndex;
import gitobject.Tree;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class JitStatus {
    public static void status(){
        String branchName = "";
        try {
            branchName = JitBranch.getNowBranchName();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("On branch " +  branchName);
        ArrayList<String> tobeCommitted = new ArrayList<>();
        ArrayList<String> untrackedFiles = new ArrayList<>();
        try {
            JitMap.setCommitMap(Commit.getLastCommit());
        } catch (IOException e) {
            e.printStackTrace();
        }
        JitMap.setIndexMap();
        for (Map.Entry<String, Integer> item : JitMap.index.getStatusValues()) {
            String keyPath = item.getKey();
            String newPath = keyPath.replace(Tree.treeSeparator, File.separator);
            if(Objects.equals(JitMap.index.getStatus(keyPath), JitIndex.newFile))
                tobeCommitted.add("\t\tnew file:\t" + newPath);
            if(Objects.equals(JitMap.index.getStatus(keyPath), JitIndex.modified))
                tobeCommitted.add("\t\tmodified:\t" + newPath);
            if(Objects.equals(JitMap.index.getStatus(keyPath), JitIndex.untracked))
                untrackedFiles.add("\t\tuntracked:\t" + newPath);
        }
        if(tobeCommitted.size() != 0){
            System.out.println("\nChanges to be committed:\n\t(use \"git rm <file>\" to unstage)\n");
            for(String line : tobeCommitted){
                System.out.println(line);
            }
        }
        if(untrackedFiles.size() != 0){
            System.out.println("\nUntracked files:\n\t(use \"git add <file>\" to include in what will be committed)\n");
            for(String line : untrackedFiles){
                System.out.println(line);
            }
        }
    }
}
